<!-- 供应商信息 -->
<template>
  <el-card header="基本信息">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="商家名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入商家名称" disabled />
      </el-form-item>
      <el-form-item label="商家地址" prop="addr">
        <el-input v-model="formData.addr" placeholder="请输入商家地址" />
      </el-form-item>
      <el-form-item label="联系人名称" prop="contactName">
        <el-input v-model="formData.contactName" placeholder="请输入联系人名称" />
      </el-form-item>
      <el-form-item label="联系人电话" prop="contactPhone">
        <el-input v-model="formData.contactPhone" placeholder="请输入联系人电话" />
      </el-form-item>
      <el-form-item label="对公账户名" prop="bankAccount">
        <el-input v-model="formData.bankAccount" placeholder="请输入对公账户名" />
      </el-form-item>
      <el-form-item label="开户行" prop="bankName">
        <el-input v-model="formData.bankName" placeholder="请输入开户行" />
      </el-form-item>
      <el-form-item label="银行账号" prop="bankNumber">
        <el-input v-model="formData.bankNumber" placeholder="请输入银行账号" />
      </el-form-item>
      <el-form-item label="商家描叙" prop="intro">
        <el-input v-model="formData.intro" placeholder="请输入商家描叙" type="textarea" />
      </el-form-item>
    </el-form>
    <div class="flex flex-row justify-center">
      <el-button @click="submitForm" type="primary" :disabled="formLoading" style="width: 200px"
        >修改</el-button
      >
    </div>
  </el-card>
</template>
<script setup lang="ts">
import { SupplierApi, SupplierVO } from '@/api/dlbz/supplier'
import { onMounted } from "vue"
import { useUserStore } from '@/store/modules/user'
const userStore = useUserStore()
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  name: undefined,
  addr: undefined,
  contactName: undefined,
  contactPhone: undefined,
  bankAccount: undefined,
  bankName: undefined,
  bankNumber: undefined,
  imgs: undefined,
  videoUrl: undefined,
  approveState: undefined,
  intro: undefined
})
const formRules = reactive({
  name: [{ required: true, message: '商家名称不能为空', trigger: 'blur' }],
  addr: [{ required: true, message: '商家地址不能为空', trigger: 'blur' }],
  contactName: [{ required: true, message: '联系人名称不能为空', trigger: 'blur' }],
  contactPhone: [{ required: true, message: '联系人电话不能为空', trigger: 'blur' }],
  bankAccount: [{ required: true, message: '对公账户名不能为空', trigger: 'blur' }],
  bankName: [{ required: true, message: '开户行不能为空', trigger: 'blur' }],
  bankNumber: [{ required: true, message: '银行账号不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as SupplierVO
    await SupplierApi.updateSupplier(data)
    message.success(t('common.updateSuccess'))
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

onMounted(async () => {
  if (userStore.getUser.relateQyId) {
    formData.value = await SupplierApi.getSupplier(userStore.getUser.relateQyId)
  }
})
</script>
